package com.ng.servive.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ng.dao.CJFXCLSDao;
import com.ng.entity.CJFXCLS;
import com.ng.servive.CjfxClsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cb
 * @Description TODO
 * @date 2023/3/30
 */
@Service
public class cjfxclsServiceImpl extends ServiceImpl<CJFXCLSDao, CJFXCLS> implements CjfxClsService {
    @Override
    public List<CJFXCLS> querycls(CJFXCLS cjfxcls) {

        List<CJFXCLS> nb=new ArrayList<CJFXCLS>();


        QueryWrapper<CJFXCLS> ss=new QueryWrapper<CJFXCLS>();
        ss.eq(cjfxcls.getGrade()!=0,"grade", cjfxcls.getGrade());

        nb= this.baseMapper.selectList(ss);
        nb=GetNode(nb);

        return nb;

    }


    private List<CJFXCLS> GetNode(List<CJFXCLS> dt)
    {
        List<CJFXCLS> ncList = new ArrayList<CJFXCLS>();

        //DataRow[] rowArray = !string.IsNullOrEmpty(classCode) ? dt.Select().Where(datarow => datarow["GRADE"].ToString().Equals("1") && datarow["Code"].ToString().Equals(classCode)).ToArray() : dt.Select().Where(datarow => datarow["GRADE"].ToString().Equals("1")).ToArray();
        List<CJFXCLS> dt2=dt.stream().filter(s->{
            return s.getGrade()==1;
        }).collect(Collectors.toList());
        for( CJFXCLS nodeitem : dt2)
        {
            CJFXCLS childnode = new CJFXCLS();
            childnode.setName(nodeitem.getName());
            childnode.setCode(nodeitem.getCode());
            childnode.setGrade(nodeitem.getGrade());
            childnode.setSyscode(nodeitem.getSyscode());
            GetChildNode(dt, childnode);
            ncList.add(childnode);
        }
        return ncList;
    }

    private void GetChildNode(List<CJFXCLS> dt, CJFXCLS node)
    {
        int grade = node.getGrade() + 1;
        List<CJFXCLS> dt2=dt.stream().filter(s->{
            return s.getGrade()==grade;
        }).collect(Collectors.toList());
        List<CJFXCLS>bb=new ArrayList<>();
        for (CJFXCLS item : dt2)
        {
            if (node.getSyscode().equals(item.getSyscode().substring(0, item.getSyscode().length() - 4)))
            {
                CJFXCLS childnode = new CJFXCLS();
                childnode.setName(item.getName());
                childnode.setCode(item.getCode());
                childnode.setGrade(item.getGrade());
                childnode.setSyscode(item.getSyscode());


                // node.child.add(childnode);
                GetChildNode(dt, childnode);
                bb.add(childnode);
                node.setChild(bb);

            }
        }
    }
}
